import App from '../App'
import { Throwable } from "../err_handle/throwable.js"
import { errHandle, onUnhandledRejection } from '../err_handle/err_handle.js'
import { errTips } from "../http/http_interceptor.js"
import { Http } from '../http/http.js'

// #ifdef MP-WEIXIN
export const localVersion = "1.0.6"
// #endif

// #ifndef MP-WEIXIN
export const localVersion = "1.0.6"
// #endif

export const serverUrl = "https://api.zhujiao.site"   //服务器域名
export const ftpUrl = "https://image.zhujiao.site"  //静态资源前缀
export const appLogo = `${ftpUrl}/ic_logo.png`    //小程序logo路径
export const apiBase = `${serverUrl}`     //接口前缀，以及动态资源前缀

export const storageTokenKey = "zhujiao-Token"			//token暂存标识
export const storageUserType = "zhujiao-UserType"			//token暂存标识
export const storageServerTimeKey = "zhujiao-ServerTime"

export const contactPhone = "13578007888"		//联系电话


export const apis = {
	//测试接口，请忽略
	getList: `${apiBase}/test/list_{start}.json`,
	
	//获取服务器当前时间
	getServerTime: `${ftpUrl}/server_time.json`,
	
	//获取配置
	getConfig: `${ftpUrl}/config.json`,
	
	//文件上传
	uploadFile: `${apiBase}/api/ums/oss/upload`,	//文件上传
	
	//获取数据字典
	getDictList: `${apiBase}/api/ums/dict/item/search/list`,		//字典分页列表
	
	//获取系统配置
	getSystemConfig: `${apiBase}/api/ums/app/sys/config/search/type/`,		//系统配置查询接口(根据类型查询)
	
	//版本更新
	getAppVersion: `${apiBase}/api/ums/appversion/now`,			//获取APP版本
	
	//登录相关
	getSmsCode: `${apiBase}/api/ums/sms/code`,			//根据手机号码获取短信验证码
	userLogin: `${apiBase}/api/ums/login`,			//系统登陆
	userLogout: `${apiBase}/api/ums/logout`,		//系统退出
	
	//用户相关
	getPlayerInfo: `${apiBase}/api/ums/app/user/load/itself`,		//用户详情（根据Token获取）
	updatePlayerInfo: `${apiBase}/api/ums/app/user/update`,			//用户修改
	
	//助教相关
	getAssistantList: `${apiBase}/api/ums/app/user/assistant/search/list`,		//助教用户列表
	getAssistantPageList: `${apiBase}/api/ums/app/user/assistant/search`,		//助教用户分页列表
	getAssistantDetail: `${apiBase}/api/ums/app/user/assistant/search/load/`,		//助教详情
	addAssistantInfo: `${apiBase}/api/ums/app/user/assistant/save`,			//助教审核新增
	getAssistantDetailToken: `${apiBase}/api/ums/app/user/assistant/load/itself`,		//助教详情（根据Token获取）
	updateAssistantDetail: `${apiBase}/api/ums/app/user/assistant/message/update`,		//助教信息修改
	assistantWriteOff: `${apiBase}/api/ums/app/user/assistant/write/off`,		//助教注销
	getAssistantQrCode: `${apiBase}/api/ums/app/user/assistant/load/qrcode`,		//助教获取下单二维码
	
	//广场视频
	addSquareAssistantVideo: `${apiBase}/api/ums/assistantshow/create`,		//上传广场图片视频
	getSquareAssistantVideo: `${apiBase}/api/ums/assistantshow/page`,		//广场列表and后台审核列表
	getAssistantVideoImage: `${apiBase}/api/ums/assistantshow/usershowlist`,		//管理后台获取用户中心图片视频
	deleteVideoImage: `${apiBase}/api/ums/assistantshow/delete`,		//删除视频或图片
	
	//app-台球厅控制层
	getPollHallList: `${apiBase}/api/ums/app/poll/hall/search/list`,		//台球厅列表
	getPollHallPageList: `${apiBase}/api/ums/app/poll/hall/search`,		//台球厅分页列表
	getPollHallDetail: `${apiBase}/api/ums/app/poll/hall/search/load/`,		//台球厅详情
	
	//app订单接口层
	addOrder: `${apiBase}/api/ums/app/order/applet/save`,		//订单新增
	getOrderMoney: `${apiBase}/api/ums/app/order/pay/calculate`,		//支付订单总价计算
	getOrderList: `${apiBase}/api/ums/app/order/search`,		//订单分页列表
	getOrderDetail: `${apiBase}/api/ums/app/order/search/load/`,		//订单详情(根据主键id查询)
	rouseOrderPay: `${apiBase}/api/ums/app/order/pay/rouse`,		//支付唤醒
	endOrderService: `${apiBase}/api/ums/app/order/assistant/end/server`,		//助教用户结束服务
	startOrderService: `${apiBase}/api/ums/app/order/assistant/start/server`,		//助教用户开始服务
	getUserOrderList: `${apiBase}/api/ums/app/order/user/search`,		//用户订单分页列表
	getAssistantOrderList: `${apiBase}/api/ums/app/order/assistant/search`,		//助教订单分页列表
	refuseOrder: `${apiBase}/api/ums/app/order/assistant/deny`,		//助教用户拒绝接单
	receiveOrder: `${apiBase}/api/ums/app/order/assistant/take`,		//助教用户接单
	renewOrder: `${apiBase}/api/ums/app/order/applet/renew`,		//小程序订单续订
	renewCalculateMoney: `${apiBase}/api/ums/app/order/renew/pay/calculate`,		//续订订单总价计算
	addOrderApp: `${apiBase}/api/ums/app/order/app/save`,			//App订单新增
	
	
	//异常订单
	abnormalSave: `${apiBase}/api/ums/app/order/abnormal/save`,		//异常订单新增
	getAbnormalOrderList: `${apiBase}/api/ums/app/order/abnormal/search`,		//异常订单分页列表
	getAbnormalOrderDetail: `${apiBase}/api/ums/app/order/abnormal/search/load/`,		//异常订单详情(根据主键id查询)
	
	//用户钱包
	getUserWallet: `${apiBase}/api/ums/assistantbill/userwallet`,		//用户钱包界面
	withdrawApply: `${apiBase}/api/ums/assistantbill/balance`,		//提现
	withdrawApplyRecord: `${apiBase}/api/ums/walletbalance/walletpage`,		//提现记录
	withdrawApplyDetail: `${apiBase}/api/ums/walletbalance/`,		//提现详情
	incomeRecordList: `${apiBase}/api/ums/walletbalance/page`,		//账单流水
	incomeRecordDetail: `${apiBase}/api/ums/assistantbill/`,		//用户入账记录详情
	
	//通用枚举类型
	getOrderAbnormalState: `${apiBase}/api/ums/general/enum/order/abnormal/state`,		//订单异常状态枚举获取
	getGradeList: `${apiBase}/api/ums/app/user/grade/search/list`,		//助教等级列表
	getReceivingList: `${apiBase}/api/ums/general/enum/user/assistant/order/receiving`,		//助教接单状态枚举
	
	//助教用户提现
	withdrawApply: `${apiBase}/api/ums/app/user/deal/pass/apply`,		//用户提现申请
	withdrawRecordList: `${apiBase}/api/ums/app/user/deal/pass/search`,		//用户提现详情列表（分页）
	withdrawRecordDetail: `${apiBase}/api/ums/app/user/deal/pass/search/list`,		//用户提现详情列表
	
	//用户余额控制层
	getBalanceInfo: `${apiBase}/api/ums/app/user/balance/search/itself`,		//用户余额详情（用户本身token）
	
	//系统引导接口层
	updateUserPosition: `${apiBase}/api/ums/app/position/update`,		//App用户经纬度数据更新
	
	//用户点赞和收藏
	addLikeFavorite: `${apiBase}/api/ums/app/user/like/favorite`,		//用户收藏-取消收藏
	addCollect: `${apiBase}/api/ums/app/user/like/like`,		//用户点赞-取消点赞
	
	
}

/**
 * 全局异常处理
 */
App.onError = errHandle
App.onUnhandledRejection = onUnhandledRejection

/**
 * 在此方法中缓存最后一次网络请求的服务器时间
 */
export const storageServerTime = function(timeStamp) {
	uni.setStorageSync(storageServerTimeKey, timeStamp)
}

/**
 * 1.拦截请求的header，在请求执行之前向header中塞数据
 * 2.需结合真实业务，修改存储登录态的key
 */
export const interceptorHeader = function(header) {
	header["Authorization"] = uni.getStorageSync(storageTokenKey)
	//玩家
	// header["authorization"] = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOjI2LCJyblN0ciI6InBTbXJ4c243alZLZFJXWXlVRGQwdXNFS1pCS0tCYmNyIn0.KWhwpUwPMBFURa-ba-TZj3YXZHscOf4RjdULPk1X6PU'
	//助教
	// header["authorization"] = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOjI4LCJyblN0ciI6ImszUjJjZGhJWjZwOXFxMVdNNXhZTTNvNXM3cUtQQ1lkIn0.PcOjc_KDfqQFY49pfKAMNgmJSD54TH51fX1Dwkq3om4'
	return Promise.resolve()
}

/**
 * 1.根据自己服务器的规则，转换接口请求结果
 * 2.需结合真实业务，根据服务器返回的响应码进行处理
 */
export const parseResponse = function(result) {
	let err_code = result.code
	if(err_code == "200") {
		return Promise.resolve(result.data)
	} else if(err_code == '407') {
		//1:未注册
		return Promise.reject(new Throwable(10002, errTips[10002]))
	} else if(err_code == "670") {
		//2:未登录
		return Promise.reject(new Throwable(10003, errTips[10003]))
	} else if(err_code == "1402") {
		//3:未选择公司
		return Promise.reject(new Throwable(10004, errTips[10004]))
	} else {
		//未知错误
		return Promise.reject(result.msg ? new Throwable(10000, result.msg):new Throwable(10000, errTips[10000]))
	}
}

Http.prototype.getPhonenumber = function(event) {
	return null
}

/**
 * 1.params结果如下
 * {
 *	 code: "", 
 *   encryptedData: "", 
 *   headerImageUrl: "", 
 *   iv: "", 
 *   nickName: "", 
 *   wxAvatarUrl: ""
 * }
 * 2.首先判断params.wxAvatarUrl不为空时，调用图片上传接口
 * 3.图片上传成功后，将返回的路径转换为绝对路径，设置给params.headerImageUrl
 * 4.然后使用params中的参数调用注册接口，调用成功后会返回token，将token保存到本地
 * 5.最后在注册成功时return Promise.resolve()即可
 * 
 * 示例：
 * let result = null
 * if(params.wxAvatarUrl) {
 *	 result = this.upload(...)
 *		.then(res => {
 *	 		let headerImage = serverUrl + res.imageUrl
 *			params.headerImageUrl = headerImage
 *			return Promise.resolve()
 *	 	})
 *      .then(res => this.request(...))
 * 		.then(res => {
 *	 		let token = res.token
 *			uni.setStorageSync("token", token)
 *			return Promise.resolve()
 *		})
 * } else {
 *	 result = this.request(...)
 *		.then(res => {
 *	 		let token = res.token
 *			uni.setStorageSync("token", token)
 *			return Promise.resolve()
 *		})
 * }
 * return result
 *
 * 注意:需要返回一个Promise对象，否则相当于没实现
 */
Http.prototype.register = function(params) {
	return Promise.resolve()
}

/**
 * 1.校验token是否过期，或者说当前登录态是否失效
 * 2.直接返回网络请求的promise对象即可
 * 
 * 示例:
 * return this.request(...)
 *
 * 注意:需要返回一个Promise对象，否则相当于没实现
 */
Http.prototype.checkLogin = function() {
	return Promise.resolve()
}

/**
 * 1.code2token以及更新token到本地的实现方法，网络请求过程中会自动调用此方法
 * 2.登录成功获取到token后，保存到本地
 * 3.最后在登录成功时return Promise.resolve()即可
 * 
 * 示例:
 * return this.request(...)
 *     .then(res => {
 *			let token = res.token
 *			uni.setStorageSync("token", token)
 *			return Promise.resolve()
 *     }) 
 *
 * 注意:需要返回一个Promise对象，否则相当于没实现
 */
Http.prototype.login = function(code) {
	return Promise.resolve()
}